﻿using System.Security.Cryptography;
using System.Text;

namespace FolderConverter.NetConsole
{
    internal class Encryptor
    {
        private static readonly string PrivateKey =
@"-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC8Ap4zx4hHux3O
FN6uVz3P6jjeg3ziPyuR6uUThab8TMHnrEXBld6itA7nh7Pq/ceRLbQv1HYlsvkB
2pmVgZjRiXUqDWjlXdAu3QQdSHN7uQlRuMaR3GQINar7eza1ZtCSV0sRCzBfjYKY
uAHD777GpfCp074eyRCfup+USX03gtssDT3ajVR7QnADtLM++hmqbUg06VN5A7ya
WTA877p6ky4TtfxTh00MGt+gytHPG2tVMLh5DKj0iAo1LTCpz3xoUjr328Ymh757
6qMEvFHC6a8kwjfbJp70Nw/dhfQzFv9i1i9N88h4EHUbXW/YPQWSunMgerribgMd
Qpgh+BhtAgMBAAECggEALZCI3M2vxoqft5ExQplcHqaKL8s4GfDrU0rVR7ztcln7
6HTE1xzGPzJaJlyqCvqEQjgMYkcgdxR/6qYa8LPe4HFq1s03Lddlm+XumFY1vrdi
fc7FORZrm9292YLMCHypcb38MF5A0/eMvgxiSSUyXBxdThtGCVn7UGq/QAXrdecy
7jc/AwiJQ2/HqAGfn1j/ZOl5AH/Vg518ytrxZOSqtcQ49rF0d7sV1U0yzxY5ecO4
KX9uRqF/vHknqUFXd36CqK7apUxtLWJzknEIlmmDWT/Z6nbQvMJgltaFHE8Iw+Bj
9WjZNCzsCLgKkg5XHy5Gy3NvqQMHO2iCdIoZFB/u1QKBgQDybPTPX/blRj1Ng+N4
P3TmFBLPu+iLVuKCDFPdIgOa54oy5b+v8jFf+UtAQRHPfqmGO2srO3cLr/5UbXrf
4P6N3CD04VOrT8uwPT1TmIgwxq6YDuSDwJ2GO07pUzKK1E/d3c41S/VuWfWuujbW
RtTIS306JNbuVLOKqjNwc+eNSwKBgQDGiaVQah9Z0bN67OHi3bovYcuSLl0nLFD2
mKmwyOnnzLCUPHIAF2Tj9e1j9XkkGPKthdUXzglNa5fgIZX3/miM0mjkJ0bmgdTo
+2hLCAutR2rcYQtIFq534PEc55TZU4qL6rjLt4bqUFOeMjNtRIjg4QQVPhEKvtPb
vBSPhhN2JwKBgDL0YdSAuDViuRnk4dfxhpGhoH6XYX6AKaXqD2qIbreNeUfPcKX7
wxMIJCACTUsOtXUoYhM5MYF8YXm/dTc/ZGHO5gJyphJ4+Dg39v7uLZoK+MGvR6rF
vpZrOt/iA9qYW50ntOMQ7ypdsD0kmMgbJThgXZbuDVsU5JP9yEgXpJDXAoGAL7BU
czJqgWqrj/VidNoJpOP6wAw+9+/fKryU1hAMj09Ngu3Z/EeiRleGRCJsjM0pTaD+
BI/LEDut6XpnyX/koR7Rbyw4DZpWB/iLnqo6E885zeHJkfFQyx7PC8Bcsq8scltZ
NCXLVHEjBLbOP4rczhd+oZOdCFW6EeflkGwGNLUCgYBzl09v2VLH5x6Gf1AYdtum
qHozhRMVLdppRysGq+RHcqDLpVU11n2ZWlLrNHusMfo97gxwADJEJIOBQ/4AoHe0
5j7mxn2oFN0Qkc04OMsYy/TFpSVcbhggX+ySqW9p4LsLytRwTmNqu5zG26sxQ4Pv
Avh1uAflYroJq8sUboyqXA==
-----END PRIVATE KEY-----
";

        private static readonly string PublicKey =
@"-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvAKeM8eIR7sdzhTerlc9
z+o43oN84j8rkerlE4Wm/EzB56xFwZXeorQO54ez6v3HkS20L9R2JbL5AdqZlYGY
0Yl1Kg1o5V3QLt0EHUhze7kJUbjGkdxkCDWq+3s2tWbQkldLEQswX42CmLgBw+++
xqXwqdO+HskQn7qflEl9N4LbLA092o1Ue0JwA7SzPvoZqm1INOlTeQO8mlkwPO+6
epMuE7X8U4dNDBrfoMrRzxtrVTC4eQyo9IgKNS0wqc98aFI699vGJoe+e+qjBLxR
wumvJMI32yae9DcP3YX0Mxb/YtYvTfPIeBB1G11v2D0FkrpzIHq64m4DHUKYIfgY
bQIDAQAB
-----END PUBLIC KEY-----
";

        /// <summary>
        /// 将明文进行加密，返回 Base64String 格式的加密字符串
        /// </summary>
        /// <param name="plainData">需要加密的明文</param>
        public static string ConvertToEncryptedBase64String(string plainData)
        {
            // 创建 RSA 实例，并加载公钥
            using (RSA rsa = RSA.Create())
            {
                rsa.ImportFromPem(PublicKey);

                // 将明文数据转换为字节数组
                byte[] plainBytes = Encoding.UTF8.GetBytes(plainData);

                // 使用公钥进行加密
                byte[] encryptedBytes = rsa.Encrypt(plainBytes, RSAEncryptionPadding.Pkcs1);

                // 转换为可读的 base64 字符串。
                var encryptedDataBase64 = Convert.ToBase64String(encryptedBytes);

                return encryptedDataBase64;
            }
        }

        /// <summary>
        /// 将 base64 加密字符串转换为明文字符串
        /// </summary>
        /// <param name="base64String"> base64 字符串</param>
        public static string ConvertFromEncryptedBase64String(string base64String)
        {
            // 创建 RSA 实例，并加载私钥
            using (RSA rsa = RSA.Create())
            {
                rsa.ImportFromPem(PrivateKey);

                // 读取密文数据
                byte[] encryptedBytes = Convert.FromBase64String(base64String);

                // 使用私钥进行解密
                byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, RSAEncryptionPadding.Pkcs1);

                // 将解密后的字节数组转换为字符串
                string decryptedData = Encoding.UTF8.GetString(decryptedBytes);

                return decryptedData;
            }
        }
    }
}
